/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tces.view;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tces.controller.CategoryDAO;
import com.tces.controller.QuestionDAO;
import com.tces.controller.SubjectScheduleDAO;
import com.tces.model.Categories;
import com.tces.model.Questions;
import com.tces.model.Students;
import com.tces.model.SubjectSchedules;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Arnold
 */
public class SubjectAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(SubjectAction.class);
    private List<SubjectSchedules> subjectScheduleList;
    private Students student;
    private String subjectScheduleID;

    @Override
    public String execute() {
        try {
            student = (Students) ActionContext.getContext().getSession().get("student");
            subjectScheduleList = SubjectScheduleDAO.getSubjectsForEvaluation(student.getStudentID());

        } catch (Exception ex) {
            log.error(ex);

        }
        return SUCCESS;
    }

    public String redirectParameters() {
        try {
            student = (Students) ActionContext.getContext().getSession().get("student");
            log.info("subjectScheduleID :" + subjectScheduleID);

        } catch (Exception ex) {
            log.error(ex);

        }
        return SUCCESS;
    }

    public String saveEvaluation() {
        try {
            student = (Students) ActionContext.getContext().getSession().get("student");
            log.info("subjectScheduleID :" + subjectScheduleID);
            if (SubjectScheduleDAO.checkDuplicate(Integer.parseInt(subjectScheduleID), student.getStudentID()) > 0) {
                if (SubjectScheduleDAO.deleteEvaluation(Integer.parseInt(subjectScheduleID), student.getStudentID()) <= 0) {
                    addActionError("Error deleting duplicate subject evaluation! Please check the log file for more info.");
                }
            }
            HttpServletRequest request = ServletActionContext.getRequest();
            List<Categories> categoryList = CategoryDAO.getActiveCategories();
            List<Questions> questionList;
            for (Categories category : categoryList) {
                questionList = QuestionDAO.getActiveQuestions(category.getCategoryID());
                for (Questions question : questionList) {
                    if (request.getParameter("c" + category.getCategoryID() + "q" + question.getQuestionID()) != null) {
                        log.info(request.getParameter("c" + category.getCategoryID() + "q" + question.getQuestionID()) + ":" + request.getParameter("c" + category.getCategoryID() + "q" + question.getQuestionID()));
                        SubjectScheduleDAO.saveEvaluation(Integer.parseInt(subjectScheduleID), student.getStudentID(), question.getQuestionID(), Integer.parseInt(request.getParameter("c" + category.getCategoryID() + "q" + question.getQuestionID())));
                    }

                }
            }
//            log.info("Suggestion: "+request.getParameter("suggestion"));
            if (SubjectScheduleDAO.checkDuplicateSuggestion(Integer.parseInt(subjectScheduleID), student.getStudentID()) > 0) {
                if (SubjectScheduleDAO.deleteEvaluationSuggestion(Integer.parseInt(subjectScheduleID), student.getStudentID()) > 0) {
                    if (!request.getParameter("suggestion").trim().isEmpty()) {
                        if (SubjectScheduleDAO.saveEvaluationSuggestion(Integer.parseInt(subjectScheduleID), student.getStudentID(), request.getParameter("suggestion")) <= 0) {
                            addActionError("Unable to save suggestion! Please check the log file for more info.");
                        }
                    }

                } else {
                    addActionError("Unable to delete the duplicate suggestions/comments.Please check the log file for more info.");
                }
            } else {
                if (!request.getParameter("suggestion").trim().isEmpty()) {
                    if (SubjectScheduleDAO.saveEvaluationSuggestion(Integer.parseInt(subjectScheduleID), student.getStudentID(), request.getParameter("suggestion")) <= 0) {
                        addActionError("Unable to save suggestion! Please check the log file for more info.");
                    }
                }
            }


        } catch (Exception ex) {
            log.error(ex);

        }
        return SUCCESS;
    }

    public List<SubjectSchedules> getSubjectScheduleList() {
        return subjectScheduleList;
    }

    public void setSubjectScheduleList(List<SubjectSchedules> subjectScheduleList) {
        this.subjectScheduleList = subjectScheduleList;
    }

    public String getSubjectScheduleID() {
        return subjectScheduleID;
    }

    public void setSubjectScheduleID(String subjectScheduleID) {
        this.subjectScheduleID = subjectScheduleID;
    }
}
